<?php

namespace App\Admin\Forms;

use App\Models\MessageRecord;
use App\Models\Order;
use App\Models\OrderService;
use App\Models\OrderServiceSubmitDetail;
use App\Support\Data;
use Dcat\Admin\Admin;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;
use Dcat\Admin\Contracts\LazyRenderable;


class SendDetailMessage extends Form implements LazyRenderable
{
    use LazyWidget; // 使用异步加载功能

    // 处理请求
    public function handle(array $input)
    {
        $id = $this->payload['id'] ?? null;

        if (!$id) {
            return $this->response()->error('参数错误');
        }

        $data = [
            'user_id' => $input['user_id'],
            'message_id' => $input['message_id'],
            'message_type' => 1,
            'admin_id' => $input['admin_id'],
            'order_id' => $input['order_id'],
        ];
        $result = MessageRecord::create($data);

        if ($result) {
            return $this->response()->success('成功')->refresh();
        } else {
            return $this->response()->error('失败');
        }
    }

    public function form()
    {
        $id = $this->payload['id'] ?? null;
        $detail = OrderServiceSubmitDetail::where('order_id', $id)->where('status', 0)->pluck('contents', 'id');
        $userid = Order::find($id)->user_id;
        // dd($userid);
        $this->hidden('order_id')->value($id);
        $this->hidden('user_id')->value($userid);
        $this->hidden('message_type')->value(1);
        $this->hidden('admin_id')->value(Admin::user()->id);
        // 获取外部传递参数
        $this->select('message_id', '消息')
            ->options($detail);
    }

    // 返回表单数据，如不需要可以删除此方法
    public function
    default()
    {
        return [
            'user_id' => '',
            'message_id'         => '', // 订单编号
            'message_type' => '',
            'admin_id' => '',
            'order_id' => '',
        ];
    }
}
